﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace BlockGame
{
    public class Settings
    {
        public Dictionary<String, Object> SettingsList { get; internal set; }
        private String SettingsPath = "settings.json";

        public Settings()
        {
            SettingsList = new Dictionary<String, Object>();
            /*
            if (File.Exists(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\" + SettingsPath))
            {
                StreamReader sr = new StreamReader(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\" + SettingsPath);
                SettingsList = JsonConvert.DeserializeObject<Dictionary<String, Object>>(sr.ReadToEnd());
                sr.Close();

                foreach (KeyValuePair<String, Object> obj in SettingsList)
                {
                    JObject _jvalue = (JObject)obj.Value;
                    Object _value = _jvalue.ToObject<Object>();
                    //SettingsList[obj.Key] = ((JObject)obj.Value).ToObject<Object>();
                }
            }
            else
            {
             * */
                // Window
                Dictionary<String, Object> WindowSettings = new Dictionary<String, Object>();
                WindowSettings.Add("Height", 50);
                WindowSettings.Add("Width", 120);
                WindowSettings.Add("ForegroundNormal", ConsoleColor.White);
                WindowSettings.Add("BackgroundNormal", ConsoleColor.Black);
                WindowSettings.Add("ForegroundHover", ConsoleColor.Blue);
                WindowSettings.Add("BackgroundHover", ConsoleColor.Black);
                WindowSettings.Add("CursorVisible", false);
                Add("Window", WindowSettings);

                // Spieleinstellungen
                Dictionary<String, Object> GameSettings = new Dictionary<String, Object>();
                GameSettings.Add("SavePath", "\\saves");
                Add("Game", GameSettings);

                // Daten
                Add("Version", new Version(0, 1, 8));
                Add("Language", "de-DE");
                Add("UpdateService", "http://dev.lkraemer.de/gameblock/update");
            /*
            }*/
        }

        public void Add(String Key, Object Value)
        {
            SettingsList.Add(Key, Value);

            StreamWriter sw = new StreamWriter(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\" + SettingsPath);
            sw.Write(JsonConvert.SerializeObject(SettingsList));
            sw.Close();
        }
    }
}
